package com.changanw.hystrix;

import com.changanw.commons.User;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/hello")
    public String hello(){
        return helloService.hello();
    }

    @GetMapping("/hello2")
    public void hello2(){
        //初始化
        HystrixRequestContext hystrixRequestContext = HystrixRequestContext.initializeContext();
        HelloCommand helloCommand = new HelloCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("changanw")),restTemplate,"changanw");
        //直接执行
        String execute = helloCommand.execute();
        System.out.println(execute);
        HelloCommand helloCommand2 = new HelloCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("changanw")),restTemplate,"changanw");
        //先入队，在执行
        final Future<String> queue = helloCommand2.queue();
        try {
            final String s = queue.get();
            System.out.println(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        hystrixRequestContext.close();
    }

    @GetMapping("/hello3")
    public void hello3(){
        final Future<String> stringFuture = helloService.hello2();
        try {
            final String s = stringFuture.get();
            System.out.println("1111"+s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/hello4")
    public void hello4(){
        //初始化
        HystrixRequestContext hystrixRequestContext = HystrixRequestContext.initializeContext();
        //第一次请求，已经缓存了
        String s  = helloService.hello3("changanw");
        //删除数据，同时清除缓存中的数据
        helloService.deleteUserByName("changanw");
        //第二次请求是，虽然还是同样的请求参数，但是缓存中已经没有了
        s  = helloService.hello3("changanw");
        hystrixRequestContext.close();

    }
    @Autowired
    UserService userService;

    @GetMapping("/hello5")
    public void hello5() throws ExecutionException, InterruptedException {
        //初始化
        HystrixRequestContext hystrixRequestContext = HystrixRequestContext.initializeContext();
        UserCollapsedCommand ucc1 = new UserCollapsedCommand(userService,99);
        UserCollapsedCommand ucc2 = new UserCollapsedCommand(userService,98);
        UserCollapsedCommand ucc3 = new UserCollapsedCommand(userService,97);
        UserCollapsedCommand ucc4 = new UserCollapsedCommand(userService,96);
        //先入队
        Future<User> queue1 = ucc1.queue();
        Future<User> queue2 = ucc2.queue();
        Future<User> queue3 = ucc3.queue();
        Future<User> queue4 = ucc4.queue();
        final User user1 = queue1.get();
        final User user2 = queue2.get();
        final User user3 = queue3.get();
        final User user4 = queue4.get();
        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user3);
        System.out.println(user4);
        hystrixRequestContext.close();
    }
    @GetMapping("/hello6")
    public void hello6() throws ExecutionException, InterruptedException {
        //初始化
        HystrixRequestContext hystrixRequestContext = HystrixRequestContext.initializeContext();
        //先入队
        Future<User> queue1 = userService.getUsers(99);
        Future<User> queue2 = userService.getUsers(98);
        Future<User> queue3 = userService.getUsers(97);

        final User user1 = queue1.get();
        final User user2 = queue2.get();
        final User user3 = queue3.get();

        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user3);
        Thread.sleep(2000);
        Future<User> queue4 = userService.getUsers(96);
        final User user4 = queue4.get();
        System.out.println(user4);
        hystrixRequestContext.close();
    }


}
